package com.netease.nim.demo.common.fragment;

import logic.util.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.inputmethod.InputMethodManager;

import com.netease.nim.demo.util.LogUtil;


public abstract class TFragment
        extends
        Fragment
{
	private static final Handler handler = new Handler();
	
	private int                  fragmentId;
	
	private boolean              destroyed;
	
	protected final boolean isDestroyed() {
		return destroyed;
	}
	
	public int getFragmentId() {
		return fragmentId;
	}
	
	public void setFragmentId(int fragmentId) {
		this.fragmentId = fragmentId;
		
	}
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		LogUtil.ui("fragment: " + getClass().getSimpleName() + " onActivityCreated()");
		
		destroyed = false;
	}
	
	public void onDestroy() {
		super.onDestroy();
		
		LogUtil.ui("fragment: " + getClass().getSimpleName() + " onDestroy()");
		
		destroyed = true;
	}
	
	protected final Handler getHandler() {
		return handler;
	}
	
	protected final void postRunnable(final Runnable runnable) {
		handler.post(new Runnable()
		{
			@Override
			public void run() {
				// validate
				// TODO use getActivity ?
				if (!isAdded()) { return; }
				
				// run
				runnable.run();
			}
		});
	}
	
	protected final void postDelayed(final Runnable runnable, long delay) {
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run() {
				// validate
				// TODO use getActivity ?
				if (!isAdded()) { return; }
				
				// run
				runnable.run();
			}
		}, delay);
	}
	
	protected void showKeyboard(boolean isShow) {
		Activity activity = getActivity();
		if (activity == null) { return; }
		
		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm == null) { return; }
		
		if (isShow)
		{
			if (activity.getCurrentFocus() == null)
			{
				imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
			}
			else
			{
				imm.showSoftInput(activity.getCurrentFocus(), 0);
			}
		}
		else
		{
			if (activity.getCurrentFocus() != null)
			{
				imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
			
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
}
